package com.htzos.common.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.htzos.admin.log.service.LogReqBaseService;
import com.htzos.admin.system.bean.SystemUserBase;
import com.htzos.common.util.CommonUtil;

/**
 * 登录后的通用菜单的拦截器，对于开放给所有用户使用的菜单验证是否已经登陆了
 *
 * @author xudz
 * @time 2017 2017年1月18日 上午10:21:25
 */
public final class LoginInterceptor extends HandlerInterceptorAdapter {

	private Logger logger=Logger.getLogger(LoginInterceptor.class);
	
	@Resource
	private LogReqBaseService logReqBaseService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean handlerOk =  super.preHandle(request, response, handler);
		
		if(!handlerOk) {
			return false;
		}
		HttpSession session = request.getSession();
		//设置基础路径
		HttpServletRequest httpRequest = request;
		HttpServletResponse httpResponse = response;
        SystemUserBase user = (SystemUserBase)session.getAttribute(CommonUtil.CURRENTUSER);
       
        if (user == null){
				httpResponse.sendRedirect(httpRequest.getContextPath() + "/toLogin"
						+ "?fromurl=" + httpRequest.getRequestURI());
				return false;
        }
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, org.springframework.web.servlet.ModelAndView modelAndView) throws Exception {
		//设置调用的地址
		super.postHandle(request, response, handler, modelAndView);
	};
	
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}

